﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LoongCAD.STC.Friendly.Scene;
using LoongCAD.STC.Friendly.Dialog;
using LoongCAD.Geometry.Friendly;
using LoongCAD.STC.Friendly;
using LoongCAD.STC;
using HelloRemoteCall;
using LoongCAD.Util;
using LoongCAD.STC.Friendly.Catalogs;
using System.Diagnostics;
using LoongCAD.STC.Friendly.Plugins;
using LoongCAD.STC.Friendly.Scene.UI;

namespace HelloUtilities.CreateRandomColumns {
    /// <summary>
    /// 独立程序UI界面应派生自ApplicationFormBase
    /// </summary>
    public partial class HelloRemoteCallForm : ApplicationFormBase {
        private HelloRemoteCallData m_data = new HelloRemoteCallData();

        public HelloRemoteCallForm() {
            InitializeComponent();

            this.InitializeForm(); //初始化窗口，创建窗口后必须执行此函数
        }

        //点击按钮"连接STC"
        private void btn_connect_Click(object sender, EventArgs e) {
            try {
                if (STCF.Document.Connect(1000))
                    this.Text = "成功连接STC";
                else
                    this.Text = "连接STC失败";
            }
            catch (TimeoutException) {
                this.Text = "连接STC超时";
            }
        }

        //点击按钮"创建随机柱"
        private void btn_create_Click(object sender, EventArgs e) {
            //先检查连接状态
            if (!STCF.Document.GetConnectionStatus()) {
                MessageBox.Show("请先连接到STC主程序");
                return;
            }

            //如有筛选CheckBox, 则将所有CheckBox设为选中以确保所有属性都被获取到
            this.SetDialogToggles(true);
            //获取UI输入并保存于m_data
            this.Modify(m_data);

            try {
                //创建用户选择器
                var picker = new Picker();

                //通过用户点选位置点创建柱零件
                for (int i = 0; i < m_data.ColumnCount; ++i) {
                    //等待用户捕捉一个点
                    var pt = picker.PickPoint($"捕捉第{i}点");

                    //在捕捉到的点处创建柱
                    this._CreateBeam(pt);
                }
            }
            catch (UserInterruptException) { //选择操作被用户中断
                MessageBox.Show("用户中断");
            }
            catch { //忽略其它错误

            }
        }

        //创建单根柱零件函数
        private void _CreateBeam(Point pos) {
            var beam = new Beam();
            beam.StartPoint = new Point(pos.X, pos.Y, -200);
            beam.EndPoint = new Point(pos.X, pos.Y, 7200);
            beam.Profile.ProfileString = m_data.Profile;
            beam.Material.MaterialString = m_data.Material;
            beam.Insert();
        }

        //点击截面型材选择框
        private void btn_profile_SelectionDone(object sender, EventArgs e) {
            tb_profile.Text = btn_profile.SelectedProfile;
        }

        //点击材质选择框
        private void btn_material_SelectionDone(object sender, EventArgs e) {
            tb_material.Text = btn_material.SelectedMaterial;
        }

    }
}
